package com.youmingxing.adpter.RecyclerViewAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.bean.Content;

import java.util.List;

/**
 * Created by chihiro on 15/12/21.
 */
public class MyContentAdapter extends RecyclerView.Adapter<MyContentAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<Content> mDatas;
    Context context;

    public MyContentAdapter(Context context, List<Content> datats) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        ImageView mImg;
        View notPassView;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_self_info_work,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.mImg = (ImageView) view
                .findViewById(R.id.selfdynamicadapter_icon);
        viewHolder.notPassView = view.findViewById(R.id.not_pass_rl);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        if (i == 0) {
            Glide.with(context).load(R.mipmap.new_diy).centerCrop()
                    .crossFade().into(viewHolder.mImg);
        } else {
            Content content = mDatas.get(i);
            if (content.status == 2) {
                viewHolder.notPassView.setVisibility(View.VISIBLE);
            } else {
                viewHolder.notPassView.setVisibility(View.INVISIBLE);

            }
            Glide.with(context).load(content.getThumbnail()).centerCrop()
                    .crossFade().into(viewHolder.mImg);
        }
        //如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });

        }
    }

    /**
     * ItemClick的回调接口
     *
     * @author zhy
     */
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
